package com.shop.cereshop.app.controller.buyer;

import com.shop.cereshop.app.page.buyer.CollectAndLikeNumber;
import com.shop.cereshop.app.page.buyer.LikeAndCollectInfo;
import com.shop.cereshop.app.page.buyer.UserDetailVO;
import com.shop.cereshop.app.page.recommend.RecommendPageVO;
import com.shop.cereshop.app.param.buyer.BrowseHistory;
import com.shop.cereshop.app.param.buyer.BuyerRelationShipVO;
import com.shop.cereshop.app.param.buyer.CereBuyerRecord;
import com.shop.cereshop.app.param.buyer.VisitorBuyerParam;
import com.shop.cereshop.app.param.recommend.CancelLikeOrCollectVo;
import com.shop.cereshop.app.param.recommend.LikeOrCollectReadParam;
import com.shop.cereshop.app.service.buyer.CereBuyerRelationShipService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerRelationship;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.buyer.CereBuyerVisitor;
import com.shop.cereshop.commons.domain.buyer.CerePetSearch;
import com.shop.cereshop.commons.domain.collect.CereBuyerCollect;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.common.PageParam;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("relationShip")
@Slf4j(topic = "BuyerRelationShip")
@Api(value = "用户关系", tags = "用户关系")
public class BuyerRelationShipController {

    @Autowired
    private CereBuyerRelationShipService cereBuyerRelationShipService;

    @PostMapping("concern")
    @ApiOperation(value = "关注用户")
    public Result<Void> getUser(BuyerRelationShipVO buyerRelationShipVO, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();


        //查询用户关系表
        CereBuyerRelationship cereBuyerRelationship = cereBuyerRelationShipService.selectBuyerRelationShip(buyerUserId, buyerRelationShipVO.getPublishPostUserId());


        if (cereBuyerRelationship == null && buyerRelationShipVO.getConcernStatus() == 1) {
            //新增用户关系
            cereBuyerRelationShipService.save(buyerRelationShipVO, buyerUserId);

        }
        //更改用户之间的关系
        else if (cereBuyerRelationship != null && buyerRelationShipVO.getConcernStatus() == 0) {
            cereBuyerRelationShipService.updateBuyerRelationShip(buyerRelationShipVO, buyerUserId);
        } else if (cereBuyerRelationship != null && buyerRelationShipVO.getConcernStatus() == 1) {
            cereBuyerRelationShipService.updateBuyerRelationShip(buyerRelationShipVO, buyerUserId);
        } else {
            return new Result<>(CoReturnFormat.PARAM_INVALID);
        }


        return new Result<>(CoReturnFormat.SUCCESS);
    }


    @GetMapping("userInfo")
    @ApiOperation(value = "查询用户信息")
    public Result<UserDetailVO> getUserInfo(@NotBlank(message = "访问用户ID不能为空")
                                            @RequestParam String visitedUserId, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();

        if (buyerUserId != null) {
            UserDetailVO userInfo = cereBuyerRelationShipService.getUserInfo(buyerUserId, visitedUserId);
            return new Result<>(userInfo, CoReturnFormat.SUCCESS);
        } else {
            return new Result<>(CoReturnFormat.USER_NOT_BUYER);
        }
    }

    @PostMapping("visited")
    @ApiOperation(value = "增加访客记录")
    public Result<Void> addVisited(@NotBlank(message = "被访问者用户id不能为空")
                                   @RequestParam Long visitedUserId, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
//        访问者用户id
        Long buyerUserId = user.getBuyerUserId();
        if (buyerUserId != null) {
            CereBuyerRecord cereBuyerRecord = new CereBuyerRecord();
            cereBuyerRecord.setVisitedUserId(visitedUserId);
            cereBuyerRecord.setVisitorUserId(buyerUserId);
            cereBuyerRelationShipService.addVisited(cereBuyerRecord);
            return new Result<>(CoReturnFormat.SUCCESS);
        } else {
            return new Result<>(CoReturnFormat.USER_NOT_BUYER);
        }
    }


    @GetMapping("visitedList")
    @ApiOperation(value = "获取访客列表")
    public Result<Page<CereBuyerVisitor>> getVisitedList(@ModelAttribute PageParam param, HttpServletRequest request) throws CoBusinessException {

        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();
        Page page = cereBuyerRelationShipService.getVisitedList(param, buyerUserId);
        return new Result<>(page, CoReturnFormat.SUCCESS);
    }

    @GetMapping("petSearch")
    @ApiOperation("宠圈搜索历史记录查询")
    public Result<Page<CerePetSearch>> petSearch(PageParam param, HttpServletRequest request) throws CoBusinessException {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();

        if (buyerUserId != null) {
            Page page = cereBuyerRelationShipService.selectHistoryPetSearch(param, buyerUserId);

            return new Result<>(page, CoReturnFormat.SUCCESS);

        }
        return new Result<>(CoReturnFormat.USER_NOT_BUYER);

    }

    @DeleteMapping("deletePetSearch")
    @ApiOperation("批量删除宠圈搜索历史记录")
    public Result<Integer> deletePetSearch(@RequestBody List<Long> searchPetIds, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();

        Integer number = 0;

        if (buyerUserId != null) {
            number = cereBuyerRelationShipService.deletePetSearch(searchPetIds, buyerUserId);

        }
        if (number > 0) {
            return new Result<>(CoReturnFormat.SUCCESS);
        }
        return new Result<>(CoReturnFormat.FAIL);
    }

    @PostMapping("addPetSearch")
    @ApiOperation("添加宠圈搜索历史记录")
    public Result<Void> addPetSearch(String searchContent, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();

        if (buyerUserId != null) {
            cereBuyerRelationShipService.addPetSearch(searchContent, buyerUserId);

        }
        return new Result<>(CoReturnFormat.SUCCESS);
    }

    @GetMapping("getCollect")
    @ApiOperation("获取收藏列表")
    public Result<Page<CereBuyerCollect>> getCollect(@ModelAttribute PageParam param, HttpServletRequest request) throws CoBusinessException {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();
        Page page = cereBuyerRelationShipService.getCollect(param, buyerUserId);
        return new Result<>(page, CoReturnFormat.SUCCESS);
    }

    @GetMapping("likeAndCollectCount")
    @ApiOperation("获取收藏和点赞数量")
    public Result<Long> likeAndCollectCount(HttpServletRequest request) throws CoBusinessException {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();
        CollectAndLikeNumber count = cereBuyerRelationShipService.likeAndCollectCount(buyerUserId);
        return new Result<>(CoReturnFormat.SUCCESS, count);
    }

    @GetMapping("likeAndCollectList")
    @ApiOperation("获取收藏和点赞列表")
    public Result<Page<LikeAndCollectInfo>> likeAndCollectList(@ModelAttribute PageParam param, HttpServletRequest request) throws CoBusinessException {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();
        Page page = cereBuyerRelationShipService.likeAndCollectList(param, buyerUserId);
        return new Result<>(page, CoReturnFormat.SUCCESS);
    }

    @PostMapping("likeAndCollect/read")
    @ApiOperation("点赞和收藏设置已读")
    public Result<Long> setReadLikeAndCollect(HttpServletRequest request, LikeOrCollectReadParam readParam) throws CoBusinessException {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();
        boolean flag = cereBuyerRelationShipService.setReadLikeAndCollect(buyerUserId, readParam);
        if (flag) {
            return new Result<>(CoReturnFormat.SUCCESS);
        }
        return new Result<>(CoReturnFormat.FAIL);
    }

    @GetMapping("userBrowseHistory")
    @ApiOperation("宠圈用户浏览历史")
    public Result<Page<RecommendPageVO>> userBrowseHistory(@ModelAttribute BrowseHistory param, HttpServletRequest request) throws CoBusinessException {
//        获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();
        if (buyerUserId.equals(param.getBuyerUserId())) {
//            分页查询浏览历史
            Page<RecommendPageVO> page = cereBuyerRelationShipService.userBrowseHistory(param);
            return new Result<>(page, CoReturnFormat.SUCCESS);
        }
        return new Result<>(CoReturnFormat.FAIL);
    }

    @DeleteMapping("deleteHistory")
    @ApiOperation("删除动态图文和视频历史浏览记录")
    public Result<Integer> deleteUserBrowseHistory(Integer type, HttpServletRequest request) throws CoBusinessException {
//        全部删除当前用户的浏览记录
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();
        Integer number = cereBuyerRelationShipService.deleteUserBrowseHistory(type, buyerUserId);
        if (number > 0) {
            return new Result<>(CoReturnFormat.SUCCESS);
        }
        throw new CoBusinessException(CoReturnFormat.FAIL);
    }

    @PostMapping("deleteUserHistory")
    @ApiOperation("批量删除浏览历史记录中的浏览用户")
    public Result<Integer> deleteUserHistory(@RequestBody List<Long> userIds, HttpServletRequest request) throws CoBusinessException {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();
        Integer number = cereBuyerRelationShipService.deleteUserHistory(userIds, buyerUserId);
        if (number > 0) {
            return new Result<>(CoReturnFormat.SUCCESS);
        }
        throw new CoBusinessException(CoReturnFormat.FAIL);
    }

    @PostMapping("deleteUserInfo")
    @ApiOperation("批量删除个人中心中的相关动态")
    public Result deleteUserInfo(@RequestBody CancelLikeOrCollectVo cancelLikeOrCollectVo, HttpServletRequest request) throws CoBusinessException {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Long buyerUserId = user.getBuyerUserId();
        Integer number = cereBuyerRelationShipService.deleteUserInfo(cancelLikeOrCollectVo.getRecommendIds(), cancelLikeOrCollectVo.getType(), buyerUserId);
        if (number > 0) {
            return new Result<>(CoReturnFormat.SUCCESS);
        }
        throw new CoBusinessException(CoReturnFormat.FAIL);
    }
}



